/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax;

import deatax.state.GameState;
import java.util.HashMap;
import java.util.LinkedList;

/**
 *
 * @author pson
 */
public class StateManager {

    private HashMap<String, GameState> states;
    private LinkedList<GameState> stack;

    public StateManager() {
        states = new HashMap<String, GameState>();
        stack = new LinkedList<GameState>();
    }
    
    public GameState getState(String name) {
        return states.get(name);
    }
    
    public void addState(String name, GameState gs) {
        states.put(name, gs);
    }

    public void pushState(GameState state) {
        if (stack.size() > 0) {
            stack.peek().pause();
        }
        stack.push(state);
        state.begin();
    }
    
    public void pushState(String name) {
        if (stack.size() > 0) {
            stack.peek().pause();
        }
        stack.push(states.get(name));
        states.get(name).begin();
    }

    public void popState() {
        if (stack.isEmpty()) {
            return;
        }
        stack.peek().die();
        stack.pop();
        if (stack.peek() != null) {
            stack.peek().resume();
        }
    }

    public GameState currentState() {
        return stack.peek();
    }
}
